package com.example.asus.dialog;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        /*第五种*/
findViewById(R.id.dialog5).setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        showSimpleDialog4();
    }
});
        /*第四种*/
        findViewById(R.id.dialog4).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showSimpleDialog3();
            }
        });
        /*第三种*/
        findViewById(R.id.dialog3).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showSimpleDialog2();
            }
        });
        /*第二种*/
        findViewById(R.id.dialog2).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
             showSimpleDialog1();
            }
        });
        /*第一种*/
        findViewById(R.id.dialog1).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showSimpleDialog();
            }
        });
    }
/*输入框*/
    private void showSimpleDialog4() {
        final EditText editText=new EditText(MainActivity.this);
        final EditText editText1=new EditText(MainActivity.this);
        View view= LayoutInflater.from(MainActivity.this).inflate
                (R.layout.dialog_input,null,false);
        final EditText nameEdit=view.findViewById(R.id.username);
        final EditText ageEdit=view.findViewById(R.id.password);
        new AlertDialog.Builder(MainActivity.this)
                .setIcon(R.mipmap.ic_launcher)
                .setTitle("请输入您的姓名年龄")
                .setView(view)
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int i) {
                       String name=nameEdit.getText().toString();
                       int age=Integer.parseInt(ageEdit.getText().toString());
                        Toast.makeText(MainActivity.this,"你输入的姓名是："+name+"；你的年龄是："+age,Toast.LENGTH_SHORT).show();

                    }
                }).create().show();
    }

    private void showSimpleDialog3() {
        final String[]hobby={"吃饭","睡觉","打豆豆"};
        new AlertDialog.Builder(MainActivity.this)
                .setIcon(R.mipmap.ic_launcher)
                .setTitle("爱好")
                .setMultiChoiceItems(hobby, null, new DialogInterface.OnMultiChoiceClickListener() {
                   StringBuffer sb=new StringBuffer();
                    @Override
                    public void onClick(DialogInterface dialog, int i, boolean isChecked) {
                        if (isChecked){
                            sb.append(hobby[i]+",");
                        }
                        Toast.makeText(MainActivity.this,"爱好为："+sb.toString(),Toast.LENGTH_SHORT).show();
                    }
                }).setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(MainActivity.this,"你已经选择好了爱好",Toast.LENGTH_SHORT).show();
            }
        }).setNeutralButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(MainActivity.this,"你已经取消了选择",Toast.LENGTH_SHORT).show();
            }
        }).create().show();
    }

    /*性别单选框*/
    private void showSimpleDialog2() {
        final String[]sex={"男","女","不确定"};
        new AlertDialog.Builder(MainActivity.this)
                .setIcon(R.mipmap.ic_launcher)
                .setTitle("你的性别是")
                .setSingleChoiceItems(sex, 1, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int i) {
                        Toast.makeText(MainActivity.this,"你的性别为："+sex[i],Toast.LENGTH_SHORT).show();
                    }
                }).setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this,"你已经输入",Toast.LENGTH_SHORT).show();
            }
        }).setNeutralButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this,"你已经取消选择",Toast.LENGTH_SHORT).show();
            }
        }).create().show();

    }

    /*多选对话框*/
    private void showSimpleDialog1() {
        final String []cities={"北京","上海","广州","深圳"};
        new AlertDialog.Builder(MainActivity.this)
                .setIcon(R.mipmap.ic_launcher)
                .setTitle("请选择你的城市")
                .setItems(cities, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int i) {
                        Toast.makeText(MainActivity.this,"你居住的城市是"+cities[i],Toast.LENGTH_SHORT).show();
                    }
                }).create().show();
    }
    /*简单对话框*/
    private void showSimpleDialog() {
        new AlertDialog.Builder(MainActivity.this)
                .setIcon(R.mipmap.ic_launcher)
                .setTitle("提示")
                .setMessage("你喜欢我吗？")
                .setPositiveButton("喜欢", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        Toast.makeText(MainActivity.this,"回答正确",Toast.LENGTH_SHORT).show();
                    }
                })
                .setNegativeButton("不喜欢", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        Toast.makeText(MainActivity.this,"输入错误",Toast.LENGTH_SHORT).show();
                    }
                })
                .setNeutralButton("没感觉", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        Toast.makeText(MainActivity.this,"请在选一次",Toast.LENGTH_SHORT).show();
                    }
                })
                .create()
                .show();
    }

}
